<?php

namespace App\Http\Controllers\AdminApi;

use App\Services\Config as ConfigService;

use Illuminate\Http\Request;

class SettingController extends BaseController
{
    public function initialize(Request $request)
    {
        parent::initialize($request);

        $this->modelName = 'option';
    }

    /**
     * 文件上传设置
     */
    public function uploadConfig(Request $request)
    {
        $className = '\\App\\Models\\' . $this->modelName;
        $model = new $className;

        if ($request->isMethod('post')) {
            $param = $request->all();
            $value = [
                'storage' => $param['storage'],
                'qiniu' => $param['qiniu'],
                'aliyun' => $param['aliyun'],
            ];
            $mimetype = [
                'image' => [
                    'max_filesize' => $param['image_size'],
                    'extensions' => $param['image_ext']
                ],
                'video' => [
                    'max_filesize' => $param['video_size'],
                    'extensions' => $param['video_ext']
                ],
                'audio' => [
                    'max_filesize' => $param['audio_size'],
                    'extensions' => $param['audio_ext']
                ],
                'file' => [
                    'max_filesize' => $param['file_size'],
                    'extensions' => $param['file_ext']
                ]
            ];
            $value['mimetype'] = $mimetype;
            if (isset($param['id']) && $param['id']) {
                $info = $model->where(['id' => $param['id']])->first();
                $info->value = json_encode($value);
                $result = $info->save();
            } else {
                $data = [
                    'name' => 'upload_setting',
                    'value' => json_encode($value)
                ];
                $result = $model->create($data);
            }

            if ($result === false) {
                return api_response(self::STATUS_ERROR, '设置失败！', []);
            }
            // 删除配置缓存
            ConfigService::delCache('upload_setting');

            return api_response(self::STATUS_SUCCESS, self::MSG_SUCCESS, []);
        }

        // 存储方式
        $storages = config('option.storages');
        // 配置
        $config = [
            'id' => '',
            'storage' => '',
            'qiniu' => [
                'accesskey' => '',
                'secretkey' => '',
                'bucket' => '',
                'url' => ''
            ],
            'aliyun' => [
                'access_key_id' => '',
                'access_key_secret' => '',
                'bucket' => '',
                'endpoint' => '',
                'bucket_domain' => ''
            ],
        ];
        $info = $model->where(['name' => 'upload_setting'])->first();
        if ($info) {
            $info['value'] = json_decode($info['value'], true);
            $config['id'] = $info['id'];
            $config['storage'] = $info['value']['storage'];
            if (isset($info['value']['qiniu'])) {
                $config['qiniu'] = $info['value']['qiniu'];
            }
            if (isset($info['value']['aliyun'])) {
                $config['aliyun'] = $info['value']['aliyun'];
            }
            $config['image_ext'] = $info['value']['mimetype']['image']['extensions'];
            $config['image_size'] = $info['value']['mimetype']['image']['max_filesize'];
            $config['video_ext'] = $info['value']['mimetype']['video']['extensions'];
            $config['video_size'] = $info['value']['mimetype']['video']['max_filesize'];
            $config['audio_ext'] = $info['value']['mimetype']['audio']['extensions'];
            $config['audio_size'] = $info['value']['mimetype']['audio']['max_filesize'];
            $config['file_ext'] = $info['value']['mimetype']['file']['extensions'];
            $config['file_size'] = $info['value']['mimetype']['file']['max_filesize'];
        }

        $data = [
            'storages' => $storages,
            'config' => $config
        ];

        return api_response(self::STATUS_SUCCESS, '', $data);
    }

    /**
     * 支付设置
     */
    public function paymentConfig(Request $request)
    {
        $className = '\\App\\Models\\' . $this->modelName;
        $model = new $className;

        if ($request->isMethod('post')) {
            return api_response(self::STATUS_SUCCESS, self::MSG_SUCCESS, []);
        }

        // 支付方式
        $payments = config('option.payments');
        // 配置
        $info = $model->where(['name' => 'payment_setting'])->first();
        $info && $info['value'] = json_decode($info['value'], true);

        $data = [
            'payments' => $payments,
            'config' => $info
        ];

        return api_response(self::STATUS_SUCCESS, '', $data);
    }
}
